package com.content.Service.Impl.artist;

import com.common.Utils.JwtUtil;
import com.content.Entity.artist.Artist;
import com.content.Entity.artist.ArtistFeatures;
import com.content.Entity.artist.ArtistHistory;
import com.content.Entity.artist.ArtistWork;
import com.content.Mapper.artist.ArtistMapper;
import com.content.Service.artist.ArtistService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class ArtistServiceImpl implements ArtistService {
    @Autowired
    private ArtistMapper artistMapper;

    @Override
    public PageInfo<Artist> getArtistList(Integer pageNum, Integer pageSize, String artistName,String categoryId,String schoolId) {
        PageHelper.startPage(pageNum,pageSize);
        List<Artist> artistList = artistMapper.getArtistList(artistName,categoryId,schoolId);
        return new PageInfo<>(artistList);
    }

    @Override
    public void addArtist(Artist artist) {
        String token = artist.getToken();
        String username = JwtUtil.extractUsername(token);
        artist.setCreateUser(username);
        artist.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        artist.setArtistId(makeArtistId());
        artistMapper.addArtist(artist);

        if (artist.getWorks() != null) {
            artistMapper.deleteArtistWork(artist.getArtistId());
            for (ArtistWork artistWork : artist.getWorks()) {
                artistWork.setWorkId(makeArtistWorkId());
                artistWork.setArtistId(artist.getArtistId());
                artistMapper.addArtistWork(artistWork);
            }
        }
        if (artist.getFeatures() != null) {
            artistMapper.deleteArtistFeatures(artist.getArtistId());
            for (ArtistFeatures artistFeatures : artist.getFeatures()) {
                artistFeatures.setShowfeaturesId(makeArtistFeaturesId());
                artistFeatures.setArtistId(artist.getArtistId());
                artistMapper.addArtistFeatures(artistFeatures);
            }
        }
        if (artist.getHistories() != null) {
            artistMapper.deleteArtistHistory(artist.getArtistId());
            for (ArtistHistory artistHistory : artist.getHistories()) {
                artistHistory.setHistoryId(makeArtistHistoryId());
                artistHistory.setArtistId(artist.getArtistId());
                artistMapper.addArtistHistory(artistHistory);
            }
        }
    }

    private String makeArtistId() {
        return "A" + System.currentTimeMillis();
    }


    @Override
    public void updateArtist(Artist artist) {
        String token = artist.getToken();
        String username = JwtUtil.extractUsername(token);
        artist.setUpdateUser(username);
        artist.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        artistMapper.updateArtist(artist);

        if (artist.getWorks() != null) {
            artistMapper.deleteArtistWork(artist.getArtistId());
            for (ArtistWork artistWork : artist.getWorks()) {
                artistWork.setWorkId(makeArtistWorkId());
                artistWork.setArtistId(artist.getArtistId());
                artistMapper.addArtistWork(artistWork);
            }
        }else{
            artistMapper.deleteArtistWork(artist.getArtistId());
        }

        if (artist.getFeatures() != null) {
            artistMapper.deleteArtistFeatures(artist.getArtistId());
            for (ArtistFeatures artistFeatures : artist.getFeatures()) {
                artistFeatures.setShowfeaturesId(makeArtistFeaturesId());
                artistFeatures.setArtistId(artist.getArtistId());
                artistMapper.addArtistFeatures(artistFeatures);
            }
        }else{
            artistMapper.deleteArtistFeatures(artist.getArtistId());
        }

        if (artist.getHistories() != null) {
            artistMapper.deleteArtistHistory(artist.getArtistId());
            for (ArtistHistory artistHistory : artist.getHistories()) {
                artistHistory.setHistoryId(makeArtistHistoryId());
                artistHistory.setArtistId(artist.getArtistId());
                artistMapper.addArtistHistory(artistHistory);
            }
        }else{
            artistMapper.deleteArtistHistory(artist.getArtistId());
        }

    }

    @Override
    public void deleteArtist(String artistId) {

        artistMapper.deleteArtist(artistId);
        artistMapper.deleteArtistWork(artistId);
        artistMapper.deleteArtistFeatures(artistId);
        artistMapper.deleteArtistHistory(artistId);
    }

    @Override
    public List<ArtistWork> getArtistWorks(String artistId) {
        return artistMapper.getArtistWorks(artistId);

    }

    @Override
    public List<ArtistFeatures> getArtistFeatures(String artistId) {
        return artistMapper.getArtistFeatures(artistId);
    }

    @Override
    public List<ArtistHistory> getArtistHistories(String artistId) {
        return artistMapper.getArtistHistories(artistId);
    }

    @Override
    public List<Artist> getArtists() {
        return artistMapper.getArtists();
    }

    @Override
    public List<Artist> getArtistsByCategoryId(String categoryId) {
        return  artistMapper.getArtistsByCategoryId(categoryId);
    }

    @Override
    public Artist getArtistById(String artistId) {
        Artist artist= artistMapper.getArtistById(artistId);
        artist.setWorks(artistMapper.getArtistWorks(artistId));
        artist.setFeatures(artistMapper.getArtistFeatures(artistId));
        artist.setHistories(artistMapper.getArtistHistories(artistId));
        return artist;
    }


    private String makeArtistWorkId() {
        return "AW" + System.currentTimeMillis();
    }

    private String makeArtistHistoryId() {
        return "AH" + System.currentTimeMillis();
    }
    private String makeArtistFeaturesId() {
        return "AF" + System.currentTimeMillis();
    }
}
